/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.miracleteam.miracle.dao.daoImpl;

import com.miracleteam.miracle.dao.NhanVienCuaGianHangDAO;
import com.miracleteam.miracle.entities.Nhanviencuagianhang;
import com.miracleteam.miracle.utils.HibernateUtil;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;


/**
 *
 * @author DangKhoa
 */
public class NhanVienCuaGianHangDAOImpl implements NhanVienCuaGianHangDAO{
     Logger logger=  Logger.getLogger(NhanVienCuaGianHangDAOImpl.class);
    public List LayDSNhanVienCuaGianHang(String maGH)
    {
        List ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select nv.khachhang from Nhanviencuagianhang nv where nv.daXoa=0 and nv.gianhang.maGianHang=:maGH";
            Query query = session.createQuery(hql).setCacheable(true);
            query.setString("maGH", maGH);
            
            ds = query.list();
        } catch (Exception e) {
            logger.error(e.getMessage());
            System.out.println(e.getMessage());
        }
        finally{
            session.close();
        }
        
        return ds;
    }
     public String PhatSinhMaNhanVienCuaGH()
    {
        String kq="NVGH-";
        Date date = new Date();
        
        kq+=String.valueOf(date.getDay());
        kq+=String.valueOf(date.getMonth());
        kq+=String.valueOf(date.getYear());
        kq+="-";
        kq+=String.valueOf(date.getHours());
        kq+=String.valueOf(date.getMinutes());
        kq+=String.valueOf(date.getSeconds());
        
        return kq;
    }
       public boolean ThemNhanVien(Nhanviencuagianhang nv)
       {
            Session session = HibernateUtil.getSessionFactory().openSession();
         if(nv==null)
             return false;
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.save(nv);
            transaction.commit();
        } catch (HibernateException ex) {
            transaction.rollback();
            logger.error(ex.getMessage());
            System.err.println(ex);
        } finally {
            session.close();
        }
        return true;
       }
      public boolean CapNhatNhanVien(Nhanviencuagianhang nv)
       {
            Session session = HibernateUtil.getSessionFactory().openSession();
         if(nv==null)
             return false;
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.update(nv);
            transaction.commit();
        } catch (HibernateException ex) {
            transaction.rollback();
            logger.error(ex.getMessage());
            System.err.println(ex);
        } finally {
            session.close();
        }
        return true;
       }
      
       public Nhanviencuagianhang KiemTraNhanVienCuaGianHang(String maGH,String ten)
    {
        Nhanviencuagianhang nv = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select nv from Nhanviencuagianhang nv where nv.daXoa=0 and nv.gianhang.maGianHang=:maGH and nv.khachhang.tenTruyCap=:tenNV";
            Query query = session.createQuery(hql).setCacheable(true);
            query.setString("maGH", maGH);
            query.setString("tenNV", ten);
            
            nv=(Nhanviencuagianhang)query.uniqueResult();
        } catch (Exception e) {
            logger.error(e.getMessage());
            System.out.println(e.getMessage());
        }
        finally{
            session.close();
        }
        
        return nv;
    }
        public int XoaDSNhanVienCuaGianHang(String ma)
    {
        int ds = 0;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "Update Nhanviencuagianhang nv set nv.daXoa=1 where nv.gianhang.maGianHang=:maGH";
            Query query = session.createQuery(hql).setCacheable(true);
            query.setString("maGH", ma);
            ds = query.executeUpdate();
        } catch (Exception e) {
            logger.error(e.getMessage());
            System.out.println(e.getMessage());
        }
        finally{
            session.close();
        }
        
        return ds;
    }
}
